﻿<%@ Page Language="C#" AutoEventWireup="true" Inherits="System.Web.UI.Page" %>

<%@ Import Namespace="Portal.Core" %>
<%@ Import Namespace="Portal.Data.Entity" %>
<div class="bot-navigation">
    <div class="jumping">
        Đến trang
        <select id="goPageTop" onchange="goPageBookByAccount(<%=BookId %>,this.options[this.selectedIndex].value,'#bookchaps');">
        </select>
    </div>
    <asp:placeholder runat="server" id="phPagingTop"></asp:placeholder>
</div>
<br class="clear" />
<asp:repeater id="Contents" runat="server">
    <HeaderTemplate>
        <table class="grid" cellpadding="0" cellspacing="0">
            <tr class="Header">
                <td style="width: 80px;">
                    Id
                </td>
                <td>
                    Đề mục. Tiêu đề
                </td>
                <td style="width: 30px;">
                    Sửa
                </td>
                <td style="width: 30px;">
                    Xóa
                </td>
            </tr>
    </HeaderTemplate>
    <FooterTemplate>
        </table>
    </FooterTemplate>
    <AlternatingItemTemplate>
        <tr class="AlterItem">
            <td>
                <%#Eval("Id")%>
            </td>
           <td style="position: relative">
                <%#Eval("Chapter")%>.
                <%#GetDisplayTitle(Eval("Title"), Eval("URL"))%>
                <div style="position: absolute;right: 5px;bottom: 5px;">
                     <%#GetBook(Eval("Book_ID"))%>
                 </div>
            </td>
            <td style="padding-left: 10px;">
                <input type="button" value="Sửa" class="CommandEdit" onclick="onChapEdit(<%#Eval("ID")%>);"/>
            </td>
            <td style="padding-left: 10px;">
               <input type="button" value="Xóa" class="CommandDelete" onclick="onChapDelete(<%#Eval("ID")%>);"/>
            </td>
        </tr>
    </AlternatingItemTemplate>
    <ItemTemplate>
        <tr class="Item">
            <td>
                <%#Eval("Id")%>
            </td>
             <td style="position: relative">
                <%#Eval("Chapter")%>.
                <%#GetDisplayTitle(Eval("Title"), Eval("URL"))%>
                 <div style="position: absolute;right: 5px;bottom: 5px;">
                     <%#GetBook(Eval("Book_ID"))%>
                 </div>
            </td>
            <td style="padding-left: 10px;">
                <input type="button" value="Sửa" class="CommandEdit" onclick="onChapEdit(<%#Eval("ID")%>);"/>
            </td>
            <td style="padding-left: 10px;">
                <input type="button" value="Xóa" class="CommandDelete" onclick="onChapDelete(<%#Eval("ID")%>);"/>
            </td>
        </tr>
    </ItemTemplate>
</asp:repeater>
<div class="bot-navigation">
    <div class="jumping">
        Đến trang
        <select id="goPageBottom" onchange="goPageBookByAccount(<%=BookId %>,this.options[this.selectedIndex].value,'#bookchaps');">
        </select>
    </div>
    <asp:placeholder runat="server" id="phPagingBottom"></asp:placeholder>
</div>
<script type="text/javascript">
    var bid = <%=BookId %> ;
    var curPage = parseInt(<%=Request["page"] %>);
    function onChapEdit(id) {
        window.open('ChapEdit.aspx?id=' + id, '_blank');
    }
    function onChapDelete(chapid) {
        if(confirmDelete()) {
            loading(true);
            $.ajax({
                url: '/Ajax.aspx',                
                data: {
                    p: 'ContentByAccount',
                    act: 'delete',
                    cid: chapid
                },
                success: function () {
                    loading(false);
                    goPageBookByAccount(bid, curPage, '#bookchaps'); //reload
                    //remove overlay
                    $('#jquery-msg-overlay').remove();
                },
                error: function (data) {loading(false); alert(data); }
            });            
        }
    }
    jQuery(function ($) {        
        //bind to select page
        var maxpage = '<%=maxpage%>';
        for (var i = 1; i <= maxpage; i++) {
            var item = '<option value="'+i+'">'+i+'</option>';
            if(curPage==i)
                item = '<option selected="selected" value="'+i+'">'+i+'</option>';
            $('#goPageTop').append(item);
            $('#goPageBottom').append(item);
        }
        
        $('#txtChapter').keyup(function (e) {
            var key = e.keyCode || e.which;
            if (key === 13) {
                goChap(bid,$(this).val());
            }
        });
        $('#txtChapterTop').keyup(function (e) {
            var key = e.keyCode || e.which;
            if (key === 13) {
                goChap(bid,$(this).val());
            }
        });
    });
</script>
<script runat="server">
    public static readonly int MAX50 = 50;
    public int BookId, maxpage;
    protected override void OnInit(EventArgs e)
    {
        if (!Portal.Data.Security.SecurityProvider.AllowCall())
            return;
       
        int.TryParse(Request["bid"], out BookId);
        var b = Book.FindByPrimaryKey(BookId);
        if (b == null)
            return;

        int page, pagesize;
        int.TryParse(Request["page"], out page);
        int.TryParse(Request["pagesize"], out pagesize);
        if (pagesize < 0) pagesize = 1;
        if (pagesize == 0 || pagesize > MAX50) pagesize = MAX50;

        int count;
        var virtalPage = page - 1;
        if (virtalPage < 0) virtalPage = 0;
        var books = Portal.Data.Entity.Content.Paging(BookId, pagesize, virtalPage, out count);
        maxpage = count / pagesize;
        if (count % pagesize != 0)
            maxpage += 1;

        if (books.Count == 0)
        {
            Response.Write("");
            Response.End();
        }
        else
        {
            Contents.DataSource = books;
            Contents.DataBind();
            Common.AddPagingToolAjax(new Control[] { phPagingTop, phPagingBottom }, count, pagesize, "#bookchaps");
        }
    }

    protected string GetUser(object eval)
    {
        PortalUser user = PortalUser.FindByPrimaryKey(eval);
        return (user != null) ? user.Name : "";
    }

    protected string GetBook(object bookId)
    {
        var book = Book.FindByPrimaryKey(bookId);
        if (book == null)
            return "null";
        return (book.Status) ? string.Format("<a target=_blank href=\"{0}\">{1}</a>", book.Url, book.FullName) : book.FullName;
    }
    protected string GetDisplayTitle(object title, object url)
    {
        return string.Format("<a class='maintitle' href='{0}' target=_blank>{1}</a>", url, title);
    }
    
</script>
